package compositepattern;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

/**
 * @author Jonny Long
 * @date 2021/6/3 8:32
 */
public class Department implements Component{
    private String name;
    // 最低工资和最高工资区间
    private int low;
    private int high;
    private Component successor;
    private List<Component> departmentsList = new LinkedList<>();

    public Department() { }

    public Department(String name) {
        this.name = name;
    }

    // 设置薪资上下限
    public void setBound(int low,int high){
        this.low=low;
        this.high=high;
    }

    public void setSuccessor(Component c){
        this.successor=c;
    }

    @Override
    public void add(Component c) {
        this.departmentsList.add(c);
    }

    @Override
    public void remove(Component c) {
        this.departmentsList.remove(c);
    }

    // 递归打印构建的树
    public void print(int depth){
        for(int i=1;i<=depth;i++){
            System.out.print("--");
        }
        System.out.println(this.name);
        for (Component department : departmentsList) {
            department.print(depth+1);
        }
    }

    @Override
    public void handlerRequest(int request) {
        if(request>=low && request<=high){
            System.out.println(this.name+">>>处理请求,加薪:"+request);
        }else if(successor!=null){
            System.out.println(this.name+">>>没有权限");
            successor.handlerRequest(request);
        }else{
            System.out.println(this.name+">>>就是不给你加薪");
        }
    }
}
